package cc.aiee.core.kit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * https://blog.csdn.net/baidu_39491409/article/details/88393879
 * </p>
 * 
 * @author YangLong
 *
 */
public class RequestReadUtils {

	private static final int BUFFER_SIZE = 1024 * 8;

	public static String read(HttpServletRequest request) throws IOException {
		BufferedReader bufferedReader = request.getReader();
		StringWriter writer = new StringWriter();
		write(bufferedReader, writer);
		return writer.getBuffer().toString();
	}

	public static long write(Reader reader, Writer writer) throws IOException {
		return write(reader, writer, BUFFER_SIZE);
	}

	public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
		int read;
		long total = 0;
		char[] buf = new char[bufferSize];
		while ((read = reader.read(buf)) != -1) {
			writer.write(buf, 0, read);
			total += read;
		}
		return total;
	}
}